import { list, find } from '../../api/warehouse'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        sysyHeight: '',
        oilStore: [],
        keyword: '',
        pageNum: 1,
        pageSize: 20,
        timer:null,
    },
    onLoad(options) {
        this.getHeight();
    },
    onShow() {
        this.data.pageNum = 1
        this.data.pageSize = 20
        this.setData({
            oilStore: [],
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize,
        })

        this.getOilList();
    },
    onPullDownRefresh() {
        wx.showLoading({
            title: '加载中',
          })
        this.data.oilStore = []
        this.data.pageNum = 1
        this.setData({
            pageNum: this.data.pageNum,
            oilStore: this.data.oilStore
        })
        setTimeout(() => {
            this.getOilList(() => {
                wx.hideLoading()
                wx.stopPullDownRefresh()
            })
        }, 1000)
    },
    onReachBottom() {
        this.data.pageNum += 1
        this.setData({
            pageNum: this.data.pageNum
        })
        this.getOilList()
    },

    getOilList(callBack) {
        let _this = this;
        let params ={
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize,
            keyword: this.data.keyword
        } 
        list(params).then(res => {
            console.log(res);
            let oilStores = res.data.data

            _this.setData({
                oilStore: oilStores,
            })
        })
         //如果回调函数存在，则执行该回调函数。
         callBack && callBack()
    },

    getHeight() {
        const sysinfo = wx.getSystemInfoSync();
        this.setData({
            sysyHeight: sysinfo.windowHeight
        })
    },
    goToDetail(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/oilStore/oilDetail?id=' + id
        })
    },
    addOilStore() {
        wx.navigateTo({
            url: '/pages/oilStore/addOilStore'
        })
    },

    keyword(e) {
        console.log(e.detail.value);
        clearTimeout(this.data.timer);
        this.data.timer = setTimeout(() => {
                this.data.keyword = e.detail.value;
                this.getOilList();
                console.log(this.data.timer);
        }, 500)
    },
    bindblur() {
        this.bindconfirm();
    },
    bindconfirm() {
        this.getOilList();
    },

    delIpt() {
        this.setData({
            keyword: ''
        })
        this.bindconfirm();
    },
})
